﻿// <copyright file="MediaRssBase.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Schema;

    #endregion

    public class MediaRssBase
    {
        #region Constants and Fields

        internal const string NS_URI = "http://search.yahoo.com/mrss/";

        internal const string PREFIX = "media";

        private readonly Dictionary<XmlQualifiedName, string> attributeExtensions = new Dictionary<XmlQualifiedName, string>();

        private readonly Collection<XElement> elementExtensions = new Collection<XElement>();

        #endregion

        #region Public Properties

        public Dictionary<XmlQualifiedName, string> AttributeExtensions
        {
            get
            {
                return this.attributeExtensions;
            }
        }

        public Collection<XElement> ElementExtensions
        {
            get
            {
                return this.elementExtensions;
            }
        }

        #endregion

        #region Public Methods

        public XmlSchema GetSchema()
        {
            return null;
        }

        #endregion

        #region Methods

        protected void AddAttributeExtensions(XmlReader reader)
        {
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToNextAttribute();
                    if (reader.NamespaceURI == "")
                    {
                        this.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                    }
                }
            }
        }

        protected void AddElementExtensions(XmlReader reader, bool isEmpty)
        {
            if (!isEmpty)
            {
                while (reader.IsStartElement())
                {
                    this.ElementExtensions.Add((XElement)XNode.ReadFrom(reader));
                }
                reader.ReadEndElement();
            }
        }

        #endregion
    }
}